<?php

namespace app\admin\controller;

use think\captcha\facade\Captcha;
use app\BaseController;
use think\Validate; // 验证器
use think\facade\Db; // 数据库的链接
use think\facade\Request;
class Login extends BaseController
{
    public function index()
    {
        $_P = input();

        $validate = new Validate;

        $validate->rule([
            'username|账户'  => 'require',
            'password|密码'     => 'require|length:6,20',
            'verificationCode|验证码'  => 'require|length:4',
        ]);

        $validate->message([
            'verificationCode.length'   => '请输入4位数的验证码',
            'password.length'     => '请设置6~20位的密码'
        ]);

        if (!$validate->check($_P)) {
            return array('code' => 500, 'msg' => $validate->getError());
        }
        if (!Captcha::check($_P['verificationCode'])) {
            return array('code' => 500, 'msg' => "验证码错误");
        }

        $adminuser_sql[] = ['username', '=', $_P['username']];

        $adminuser_sql[] = ['password', '=', md5($_P['password'])];

        $adminuser = Db::name('admin')->where($adminuser_sql)->find();

        // 获取ip地址
        $ip = Request::instance()->ip();
        if (empty($adminuser)) {
            return array('code' => 500, 'msg' => "登录失败");
        } else {
            // 更新登录时间和ip
            Db::name('admin')->where('id', $adminuser['id'])->update(['ip' => $ip]);
            return array('code' => 200, 'msg' => "登录成功", "data" => array('token' => createToken($adminuser['id'], md5($_P['password']))));
        }
    }
    /**
     * 后台登录页面的验证码
     * return 返回验证码图形
     */
    public function captcha()
    {
        return Captcha::create();
    }
    public function userInfo()
    {
        header('Access-Control-Allow-Origin: *');
        // 通过token获取登录用户的质料
        $token = checkToken(str_replace("Bearer ", "", $_SERVER['HTTP_AUTHORIZATION']));
        if ($token['code'] != 200) {
            return array('code' => 500, 'msg' => $token['msg']);
        }
        $adminuser = Db::name('admin')->where('id', $token['data']['UserID'])->find();

        return array('code' => 200, 'msg' => "success", 'data' => array('roles' => array($adminuser['roles']), 'ability' => array("WRITE", "DELETE"), "username" => $adminuser['username'], "id" => $adminuser['id'], "avatar" => $adminuser['avatar']));
    }
    public function userInfos()
    {
        header('Access-Control-Allow-Origin: *');
        // 通过token获取登录用户的质料
        $token = checkToken(str_replace("Bearer ", "", $_SERVER['HTTP_AUTHORIZATION']));
        if ($token['code'] != 200) {
            return array('code' => 500, 'msg' => $token['msg']);
        }
        $adminuser = Db::name('admin')->where('id', $token['data']['UserID'])->find();

        if (empty($adminuser)) {
            return array('code' => 500, 'msg' => "账号异常");
        } else {
            $adminuser['pass'] = '';
            return array('code' => 200, 'msg' => "success", 'data' => $adminuser, 'type' => 'admin');
        }
    }
    public function doEdit()
    {
        $get = input();

        $AdminId = request()->AdminId;
        if (!empty($get['pass'])) {
            Db::name('admin')->where('id', $AdminId)->update(['password' => md5($get['data']['pass'])]);
            return array('code' => 200, 'msg' => '更新成功');
        }
        
    }
    // 退出登录

    public function logout()
    {
        return array('code' => 200, 'msg' => '退出成功');
    }
}
